/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client.gui;

import client.meta.FileSlot;
import clientside.ControlClientSide;
import clientside.DataClientSide;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author USER
 */
public class MainView extends JFrame {

	private JMenuBar mainMenuBar;
	private JMenu menuA;
	private JPanel mainToolbar;
	// toolbar buttons
	private JButton btnCreateDicFile;
	private JButton btnAddNewDownload;
	private JButton btnStartDownload;
	private JButton btnPauseDownload;
	private JButton btnStopDownload;
	private JRadioButton rdnShowAll;
	private JRadioButton rdnShowCompleted;
	private JRadioButton rdnShowDownloading;
	private JRadioButton rdnShowSeeding;
	// end toolbar buttons
	private JTextField statusBar;
	/**
	 * The main panel which contains the download panel and info panel
	 */
	private JSplitPane mainPanel;
	private JPanel downloadListPanel;
	private JPanel infoPanel;
	private JTable downloadListTable;
	private UploadView uploadWnd;
	//
	
	private ControlClientSide controlThread;
	private DataClientSide dataThread;

	public MainView(ControlClientSide cThread, DataClientSide dThread) {
		super( "P2P sharing program - Computer Networking" );

		this.controlThread = cThread;
		this.dataThread = dThread;
		this.setLayout( new BorderLayout( 2, 2 ) );

		mainMenuBar = new JMenuBar();

		menuA = new JMenu( "Menu A" );
		mainMenuBar.add( menuA );

		this.setJMenuBar( mainMenuBar );

		// toolbar setup
		mainToolbar = new JPanel( new BorderLayout() );

		JPanel toolbarButtonPnl = new JPanel( new FlowLayout( FlowLayout.LEFT ) );

		btnCreateDicFile = new JButton( "Create DicFile " );
		btnCreateDicFile.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed( ActionEvent e ) {
				uploadWnd = new UploadView( btnCreateDicFile, controlThread, dataThread );
				uploadWnd.pack();
				uploadWnd.setResizable( false );
				uploadWnd.setLocationRelativeTo( MainView.this );
				uploadWnd.setVisible( true );
			}
		} );
		toolbarButtonPnl.add( btnCreateDicFile );

		btnAddNewDownload = new JButton( "New download" );
		btnAddNewDownload.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed( ActionEvent e ) {
				addNewDownload();
			}
		} );
		toolbarButtonPnl.add( btnAddNewDownload );

		btnStartDownload = new JButton( "Start" );

		toolbarButtonPnl.add( btnStartDownload );

		btnPauseDownload = new JButton( "Pause" );

		toolbarButtonPnl.add( btnPauseDownload );

		btnStopDownload = new JButton( "Stop" );

		toolbarButtonPnl.add( btnStopDownload );

		JPanel toolbarDownSwitchPnl = new JPanel( new FlowLayout( FlowLayout.RIGHT ) );
//
//		rdnShowAll = new JRadioButton( "Show all" );
//		rdnShowAll.setSelected( true );
//		rdnShowDownloading = new JRadioButton( "Downloading" );
//		rdnShowSeeding = new JRadioButton( "Seeding" );
//		rdnShowCompleted = new JRadioButton( "Completed" );
//
//		ButtonGroup group = new ButtonGroup();
//		group.add( rdnShowAll );
//		group.add( rdnShowDownloading );
//		group.add( rdnShowSeeding );
//		group.add( rdnShowCompleted );
//
//		toolbarDownSwitchPnl.add( rdnShowAll );
//		toolbarDownSwitchPnl.add( rdnShowDownloading );
//		toolbarDownSwitchPnl.add( rdnShowSeeding );
//		toolbarDownSwitchPnl.add( rdnShowCompleted );

		mainToolbar.add( toolbarButtonPnl, BorderLayout.LINE_START );
		mainToolbar.add( toolbarDownSwitchPnl, BorderLayout.LINE_END );

		this.add( mainToolbar, BorderLayout.PAGE_START );
		// end toolbar setup

		statusBar = new JTextField( "The program is ready" );
		statusBar.setEnabled( false );

		this.add( statusBar, BorderLayout.PAGE_END );

		downloadListPanel = new JPanel();
		downloadListPanel.setLayout( new BorderLayout() );
		downloadListPanel.setMinimumSize( new Dimension( this.getWidth(), 100 ) );

		MainTableModel downloadListModel = new MainTableModel();
		downloadListTable = new JTable( downloadListModel );
		downloadListTable.setFillsViewportHeight( true );

		downloadListPanel.add( downloadListTable.getTableHeader(), BorderLayout.PAGE_START );
		downloadListPanel.add( downloadListTable );

		infoPanel = new InfoPanel();

		mainPanel = new JSplitPane( JSplitPane.VERTICAL_SPLIT, downloadListPanel, infoPanel );
		mainPanel.setDividerLocation( 300 );

		this.getContentPane().add( mainPanel );

		// demo
		downloadListModel.insertLast( new FileSlot( 2, "FIle name", "/home/trunghieu/bin", 3467, "ready", 0.1, "" ) );

		// end demo
	}

	private void addNewDownload() {
		JFrame addDownloadDlg = new JFrame( "Create new download" );
		JPanel mainAddDownload = new JPanel();
		mainAddDownload.setBorder( new EmptyBorder( 6, 6, 6, 6 ) );
		addDownloadDlg.getContentPane().add( mainAddDownload );
		mainAddDownload.setLayout( new GridBagLayout() );
		GridBagConstraints c = new GridBagConstraints();
		c.ipadx = 6;
		c.ipady = 6;
		c.anchor = GridBagConstraints.LINE_START;

		JLabel lblSelectDicFile = new JLabel( "Choose the Dic file" );
		c.gridx = 0;
		c.gridy = 0;
		mainAddDownload.add( lblSelectDicFile, c );

		JTextField txtSelectDicFile = new JTextField();
		txtSelectDicFile.setPreferredSize( new Dimension( 600, 24 ) );
		c.gridx = 1;
		c.gridy = 0;
		mainAddDownload.add( txtSelectDicFile, c );

		JButton btnSelectDicFile = new JButton( "Browse ..." );
		c.gridx = 2;
		c.gridy = 0;
		mainAddDownload.add( btnSelectDicFile, c );

		JLabel lblSelectSaveFile = new JLabel( "Save your file to" );
		c.gridx = 0;
		c.gridy = 1;
		mainAddDownload.add( lblSelectSaveFile, c );

		JTextField txtSelectSaveFile = new JTextField();
		txtSelectSaveFile.setPreferredSize( new Dimension( 600, 24 ) );
		c.gridx = 1;
		c.gridy = 1;
		mainAddDownload.add( txtSelectSaveFile, c );

		JButton btnSelectSaveFile = new JButton( "Browse ..." );
		c.gridx = 2;
		c.gridy = 1;
		mainAddDownload.add( btnSelectSaveFile, c );

		JPanel addFooterPanel = new JPanel( new FlowLayout( FlowLayout.RIGHT ) );
		JButton btnOK = new JButton( "OK" );
		addFooterPanel.add( btnOK );
		JButton btnCancel = new JButton( "Cancel" );
		addFooterPanel.add( btnCancel );
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 3;
		c.insets = new Insets( 20, 0, 0, 0 );
		c.fill = GridBagConstraints.HORIZONTAL;
		mainAddDownload.add( addFooterPanel, c );

		addDownloadDlg.setLocation( 20, 200 );
		addDownloadDlg.pack();
		addDownloadDlg.setVisible( true );

	}

	private static class MainTableModel extends AbstractTableModel {

		private String[] columnNames = { FileSlot.KEY_FILENAME, FileSlot.KEY_FILESIZE, FileSlot.KEY_STATUS,
										 FileSlot.KEY_DOWNSPD, FileSlot.KEY_DOWNLOADED, FileSlot.KEY_EST_TIME,
										 FileSlot.KEY_UPSPD, FileSlot.KEY_UPLOADED, FileSlot.KEY_UD_RATIO };
		private List<FileSlot> data = new ArrayList<FileSlot>();

		@Override
		public int getRowCount() {
			return data.size();
		}

		@Override
		public int getColumnCount() {
			return columnNames.length;
		}

		@Override
		public Object getValueAt( int rowIndex, int columnIndex ) {
			FileSlot item = data.get( rowIndex );
			String ret = "";
			switch ( columnIndex ) {
				case 0:
					ret = item.getFileName();
					break;
				case 1:
					ret = Long.toString( item.getFileSize() / 1024 ) + "KB";
					break;
				case 2:
					ret = item.getStatus();
					break;
				case 3:
					ret = Long.toString( item.getDownSpeed() / 1000 ) + "KB/s";
					break;
				case 4:
					ret = Long.toString( item.getDownloaded() / 1024 ) + "KB";
					break;
				case 5:
					long val = item.getEstTime();
					if ( val == Long.MAX_VALUE ) {
						ret = "N/A";
					}
					else {
						ret = Long.toString( item.getEstTime() ) + "s";
					}
					break;
				case 6:
					ret = Long.toString( item.getUpSpeed() / 1000 ) + "KB/s";
					break;
				case 7:
					ret = Long.toString( item.getUploaded() / 1024 ) + "KB";
					break;
				case 8:
					
					ret = Double.toString( item.getUpdownRatio() );
					break;
				default:

					break;
			}
			return ret;
		}

		@Override
		public String getColumnName( int col ) {
			return columnNames[col];
		}

		@Override
		public Class getColumnClass( int c ) {
			return getValueAt( 0, c ).getClass();
		}

		public void insert( int index, FileSlot obj ) {
			data.add( index, obj );
		}

		public void insertLast( FileSlot obj ) {
			data.add( obj );
		}
	}
}
